package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.CityUniversity;
import com.zhan.ielts.domain.query.CityUniversityQuery;
import com.zhan.ielts.domain.vo.CityUniversityVo;
import com.zhan.ielts.service.CityUniversityService;

@Controller
@RequestMapping("/city/university")
public class CityUniversityController extends BaseControllerImpl<CityUniversity, CityUniversityQuery> {

	private Logger log = LoggerFactory.getLogger(CityUniversityController.class);

	@Autowired
	private CityUniversityService cityUniversityService;

	@Override
	protected BaseService<CityUniversity> getBaseService() {
		return cityUniversityService;
	}

	@SuppressWarnings({ "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchCityUniversity(String userId) {
		log.debug("获取城市及大学开始");

		// 检查userId 是否为空
		if (StringUtils.isEmpty(userId)) {
			// return new Result(Status.ERROR, "userId为空！");
		}

		// 检查userId 是否有效
		// Users query = new Users();
		// query.setId(data.getUserId());
		// if (usersService.queryCount(query) < 1) {
		// // return new Result(Status.ERROR, "userId无效！");
		// }

		Map map = new HashMap();
		try {
			CityUniversityVo cityUniversity = new CityUniversityVo();
			Sort sort = new Sort(Direction.DESC, "cityCode");
			Pageable pageable = new PageRequest(0, 100, sort);

			map = cityUniversityService.fetchCityUniversity(cityUniversity, pageable);
			log.debug("获取城市及大学结束");
		} catch (Exception e) {
			e.printStackTrace();
			// return new Result(Status.ERROR, "网络或系统错误！");
			return new CustomResult(0, "网络或系统错误！", map);
		}

		// return new Result(Status.OK, map);
		return new CustomResult(1, "", map);

	}



}
